﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;

namespace ContractManager.Common.Class
{
    public  class CustomRuleByRegex: ValidationRule 
    { 
        String regex;
         bool isnotblank;
       
        public  static  String StrIsNumeric ="^[\\+\\-?.?][0-9]*\\[0-9]+$";

        public static String StrPhone = @"(^(\(\d{2,3}\))|(\d{3}\-))(\(0\d{2,3}\)|??0\d{2,3}-)[1-9]\d{6,7}(\-\d{1,4})$|?(1(([35][0-9])|(47)|[8][01236789]))\d{8}$"; 

       
        public CustomRuleByRegex ( String regex, bool isnotblank) 
        { 
            this.regex =regex;
             this.isnotblank = isnotblank; 
        } 

        public  override  bool Validate (Control control, object value) 
        { 
            bool flag;
             try 
            { 
                if (value == null || value.ToString ().Trim() ==String.Empty) 
                { 
                    if (isnotblank) 
                    { 
                        flag = true ; 
                    } 
                    else 
                    { 
                        flag = false ;
                        this.ErrorText =" The value is not allowed to be empty! " ; 
                    } 
                } 
                else 
                { 
                    flag = Regex.IsMatch (( String ) value, regex); 
                } 
            }
             catch (Exception ex) 
            { 
                this.ErrorText =ex.Message; 
                flag = false ; 
            } 
            return flag; 
        } 
    }
}
